﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MyOA.Model;

namespace MyOA.DAL
{
    public class ManualSignService
    {
        DBHelper db = new DBHelper();
        UserInfoService userInfoService = new UserInfoService();

        /// <summary>
        /// 根据SQL语句查询员工信息
        /// </summary>
        /// <param name="safeSql"></param>
        /// <returns></returns>
        private IList<ManualSign> GetManualSignsBySql(string safeSql)
        {
            List<ManualSign> list = new List<ManualSign>();
            string userId;
            try
            {
                DataTable table = db.getDataTable(safeSql);

                foreach (DataRow row in table.Rows)
                {
                    ManualSign manualSign = new ManualSign();

                    manualSign.SignId = (int)row["SignId"];
                    userId = (string)row["UserId"];
                    manualSign.SignTime = (DateTime)row["SignTime"];
                    manualSign.SignDesc = (string)row["SignDesc"];
                    manualSign.SignTag = (int)row["SignTag"];

                    list.Add(manualSign);
                }
                db.CloseConnection();
                return list;

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }


        /// <summary>
        /// 添加签到信息
        /// </summary>
        /// <param name="manualSign"></param>
        /// <returns>返回新插入员工打卡信息Id</returns>
        public int AddManualSign(ManualSign manualSign)
        {
            try
            {
                SqlParameter[] para = new SqlParameter[]
            {
                new SqlParameter("@SignTime",manualSign.SignTime),
                new SqlParameter("@AccessorySize",manualSign.AccessorySize),
                new SqlParameter("@SignDesc",manualSign.SignDesc),
                new SqlParameter("@SignTag",manualSign.SignTag),
                new SqlParameter("@userId",manualSign.User.UserId)
            };
                int newId = Convert.ToInt32(db.getCount("sp_insertManualSign", para));//调用存储过程
                return newId;

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }

        /// <summary>
        /// 根据ID查询员工信息
        /// </summary>
        /// <param name="signId"></param>
        /// <returns></returns>
        public static ManualSign GetSignId(int signId)
        {
            DBHelper helper = new DBHelper();
            ManualSign ms = new ManualSign();
            using (SqlDataReader reader = helper.getDataReader("proc_GetSignId", new SqlParameter("@signId", signId)))
            {
                if (reader.Read())
                {
                    ms.SignId = (int)reader["SignId"];
                    ms.SignTime = (DateTime)reader["SignTime"];
                    ms.AccessorySize = (int)reader["Accessorysize"];
                    ms.SignDesc = reader["SignDesc"].ToString();
                    ms.SignTag = (int)reader["SignTag"];
                }
            }
            helper.CloseConnection();
            return ms;
        }

        /// <summary>
        /// 根据用户ID查询用户信息
        /// </summary>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public ManualSign GetManualSignInOf(string loginId)
        {
            ManualSign ms = null;

            using (SqlDataReader reader = db.getDataReader("sp_getManualSign", new SqlParameter("@loginId", loginId)))
            {
                while (reader.Read())
                {
                    ms = new ManualSign();
                    string UserName = reader["UserName"].ToString();
                    ms.User = userInfoService.GetUserInfoByUserName(UserName);
                    ms.SignDesc = reader["SignDesc"].ToString();
                }
                db.CloseConnection();
                reader.Close();
                return ms;
            }
        }
    }
}
